<?php

namespace app\admin\controller;

class SalesBase
{
    public function index()
    {
        $query = \app\model\Salesman::field(['id', 'real_name', 'company_id', 'phone', 'invoice_tax', 'created_at'])->with(['company'])->order('id', 'desc');

        if (request()->has('realName')) {
            $query->whereLike('real_name', '%' . input('realName') . '%');
        }

        if (request()->has('phone')) {
            $query->whereLike('phone', '%' . input('phone') . '%');
        }

        if (request()->has('createdAt')) {
            $query->whereBetween('created_at', input('createdAt'));
        }

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['salesmans' => $models]]);
    }

    public function delete()
    {
        $model = \app\model\Salesman::destroy(input('id'));

        return json(['code' => 20000, 'msg' => '移除成功', 'data' => $model]);
    }

    public function update()
    {
        $model = \app\model\Salesman::update(input());

        if ($model->id) {
            $model = \app\model\Salesman::find($model->id);
        }

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => $model]);
    }

    public function create()
    {
        $param = request()->param();

        $model = \app\model\Salesman::create($param);

        if (!$model) {
            return json(['code' => 40400, 'msg' => '新增失败']);
        }

        return json(['code' => 20000, 'msg' => '新增成功', 'data' => $model]);
    }
}
